package com.hades.xq.miaoshao.controller;

import com.hades.xq.miaoshao.controller.viewobject.ItemVo;
import com.hades.xq.miaoshao.error.BusinessException;
import com.hades.xq.miaoshao.response.CommonReturnType;
import com.hades.xq.miaoshao.service.ItemService;
import com.hades.xq.miaoshao.service.model.ItemModel;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Controller("/item")
@RequestMapping("/item")
@Slf4j
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = CONSUMES_TYPE_FORMED)
    @ResponseBody
    public CommonReturnType createItem(@RequestParam(name = "title") String title,
                                       @RequestParam(name = "description") String description,
                                       @RequestParam(name = "price") BigDecimal price,
                                       @RequestParam(name = "stock") Integer stock,
                                       @RequestParam(name = "imgUrl") String imgUrl) throws BusinessException {
        //封装service请求用来创建商品
        ItemModel itemModel = new ItemModel();
        itemModel.setTitle(title);
        itemModel.setDescription(description);
        itemModel.setPrice(price);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);
        ItemModel result = itemService.createItem(itemModel);
        ItemVo vo = convertVoFromModel(result);
        return CommonReturnType.create(vo);
    }

    @RequestMapping(value = "/getItemInfo", method = RequestMethod.GET)
    @ResponseBody
    public CommonReturnType getItemInfo(@RequestParam(name = "id") Integer id) {
        ItemModel itemModel = itemService.getItemById(id);
        log.info("getItemInfo().itemModel->" + itemModel.toString());
        ItemVo vo = convertVoFromModel(itemModel);
        return CommonReturnType.create(vo);
    }

    @RequestMapping(value = "/getItemList", method = RequestMethod.GET)
    @ResponseBody
    public CommonReturnType getItemList() {
        List<ItemModel> list = itemService.listItem();
        List<ItemVo> itemVoList = list.stream().map(itemModel -> {
            ItemVo itemVo = this.convertVoFromModel(itemModel);
            return itemVo;
        }).collect(Collectors.toList());
        return CommonReturnType.create(itemVoList);
    }


    private ItemVo convertVoFromModel(ItemModel itemModel) {
        if (itemModel == null) {
            return null;
        }
        ItemVo itemVo = new ItemVo();
        BeanUtils.copyProperties(itemModel, itemVo);
        if (itemModel.getPromoModel() != null) {
            itemVo.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVo.setPromoId(itemModel.getPromoModel().getId());
            itemVo.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
//            itemVo.setStartTime(itemModel.getPromoModel().getStartTime().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
            itemVo.setStartTime(itemModel.getPromoModel().getStartTime().toString("yyyy-MM-dd HH:mm:ss"));
        } else {
            itemVo.setPromoStatus(0);
        }

        return itemVo;
    }
}